﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace LANmine
{
    public partial class ProfileForm : Form
    {
        private Language lmLang;
        private User lmUser;
        private String[] langKeys;

        Form frmEditPassword;

        public ProfileForm()
        {
            InitializeComponent();
        }

        public ProfileForm(Language lmLang, User lmUser)
            : this()
        {
            /** Save the language and user objects */
            this.lmLang = lmLang;
            this.lmUser = lmUser;

            /** Dock the window */
            this.Dock = DockStyle.Fill;

            ApplyLanguage();

            /** Populate text fields */
            txtFirstname.Text = lmUser.FirstName;
            txtName.Text = lmUser.LastName;
            txtUsername.Text = lmUser.UserName;
            txtEmail.Text = lmUser.Email;

            /** Insert all languages from the Language class */
            for (int i = 0; i < lmLang.LangDict.Count; i++)
            {
                /** Add an item from the Language Dictionary */
                cboLanguage.Items.Add(lmLang.LangDict[i].ToString());

                /** Automagically select the user's current language */
                if(lmLang.LangDict[i].ToString() == lmLang.LangDict[lmUser.Language].ToString())
                    cboLanguage.SelectedIndex = i;
            }

            /** Create a key collection */
            langKeys = new String[lmLang.LangDict.Keys.Count];
            lmLang.LangDict.Keys.CopyTo(langKeys, 0);

        }

        private Boolean UpdateProfile()
        {
            Database dbProf = new Database("rw");
            SqlConnection dbConn = dbProf.getDbConn();
            DataSet dsProf = new DataSet();
            DataTable dtProf = new DataTable("deelnemers");

            /** Open DB connection to fetch */
            dbConn.Open();

            /** Create data adapters and command builders */
            SqlDataAdapter daProf = new SqlDataAdapter("SELECT TOP 1 * FROM deelnemers WHERE deelnemers.id='" + lmUser.ID + "'", dbConn);
            SqlCommandBuilder cbProf = new SqlCommandBuilder(daProf);

            /** Fill the datatable */
            daProf.Fill(dtProf);
            dsProf.Tables.Add(dtProf);

            /** Close the DB connection */
            dbConn.Close();

            /** Modify DataSet */
            dsProf.Tables["deelnemers"].Rows[0]["email"] = txtEmail.Text.ToString();
            dsProf.Tables["deelnemers"].Rows[0]["taal"] = langKeys[cboLanguage.SelectedIndex];

            /** Open the DB to put */
            dbConn.Open();
            /** Update the record */
            daProf.Update(dsProf, "deelnemers");
            /** Close the DB */
            dbConn.Close();

            return true;
        }

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite all text properties of all elements in all forms with the right ones.
         */
        private void ApplyLanguage()
        {
            /**
             * Header label
             */
            this.lblProfile.Text = this.lmLang.GetString("menu.btnProfile");
            /**
             * other labels & buttons
             */
            this.lblName.Text = this.lmLang.GetString("profile.lblName");
            this.lblUsername.Text = this.lmLang.GetString("profile.lblUsername");
            this.lblEmail.Text = this.lmLang.GetString("profile.lblEmail");
            this.lblLanguage.Text = this.lmLang.GetString("profile.lblLanguage");
            this.btnChangePassword.Text = this.lmLang.GetString("profile.btnChangePassword");
            this.btnSave.Text = this.lmLang.GetString("profile.btnSave");
            this.lblText.Text = this.lmLang.GetString("profile.lblText");
        }
        #endregion

        #region Event Handlers

        private void tlpProfile_Resize(object sender, EventArgs e)
        {
            ///** Resize components */
            //txtFirstname.Width = tlpProfile.Width / 4;
            //txtName.Width = tlpProfile.Width / 4;
            //txtEmail.Width = tlpProfile.Width / 4;
            //txtUsername.Width = tlpProfile.Width / 4;
            //cboLanguage.Width = tlpProfile.Width / 4;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateProfile())
            {
                MessageBox.Show(this.lmLang.GetString("profile.msgUpdateSucceeded"));
            }
            else
            {
                MessageBox.Show(this.lmLang.GetString("profile.msgUpdateFailed"));
            }
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            //spawn EditPasswordForm as Dialog

            this.frmEditPassword = new EditPasswordForm(lmUser.UserName, lmLang);
            this.frmEditPassword.Text = this.lmLang.GetString("editpass.text");
            this.frmEditPassword.ShowDialog();
        }

        #endregion
    }
}
